part of '../repository.dart';

const _kPurchasedSubscriptionInfoUserCancellationTimeKey = 'time';
const _kPurchasedSubscriptionInfoUserCancellationSurveyResultKey = 'survey_result';

const _kPurchasedSubscriptionInfoUserCancellationInfoTypeValue = 'user';

class _PurchasedSubscriptionInfoUserCancellationInfoDto extends Equatable
    implements _PurchasedSubscriptionInfoCancellationInfoDto {
  const _PurchasedSubscriptionInfoUserCancellationInfoDto._internal({
    required this.timeDto,
    required this.surveyResultDto,
  });

  final _TimeDto timeDto;
  final _PurchasedSubscriptionInfoCancellationSurveyResultDto surveyResultDto;

  @override
  String get type => _kPurchasedSubscriptionInfoUserCancellationInfoTypeValue;

  @override
  List<Object?> get props {
    return [
      timeDto,
      surveyResultDto,
    ];
  }

  factory _PurchasedSubscriptionInfoUserCancellationInfoDto.fromJson(Map<String, dynamic> json) {
    final timeString = json[_kPurchasedSubscriptionInfoUserCancellationTimeKey] as String;
    final timeDto = _TimeDto.fromString(timeString);

    final surveyResultJson =
        json[_kPurchasedSubscriptionInfoUserCancellationSurveyResultKey] as Map<String, dynamic>;
    final surveyResultDto =
        _PurchasedSubscriptionInfoCancellationSurveyResultDto.fromJson(surveyResultJson);

    return _PurchasedSubscriptionInfoUserCancellationInfoDto._internal(
      timeDto: timeDto,
      surveyResultDto: surveyResultDto,
    );
  }

  factory _PurchasedSubscriptionInfoUserCancellationInfoDto.fromEntity(
      GooglePlayPurchasedSubscriptionInfoUserCancellationInfo cancellationInfo) {
    final timeDto = _TimeDto.fromEntity(cancellationInfo.time);

    final surveyResultDto =
        _PurchasedSubscriptionInfoCancellationSurveyResultDto.fromEntity(cancellationInfo.surveyResult);

    return _PurchasedSubscriptionInfoUserCancellationInfoDto._internal(
      timeDto: timeDto,
      surveyResultDto: surveyResultDto,
    );
  }

  @override
  Map<String, dynamic> toJson() {
    return {
      _kPurchasedSubscriptionInfoCancellationInfoTypeKey: type,
      _kPurchasedSubscriptionInfoUserCancellationTimeKey: timeDto.toString(),
      _kPurchasedSubscriptionInfoUserCancellationSurveyResultKey: surveyResultDto.toJson(),
    };
  }

  @override
  GooglePlayPurchasedSubscriptionInfoUserCancellationInfo toEntity() {
    return GooglePlayPurchasedSubscriptionInfoUserCancellationInfo(
      time: timeDto.toEntity(),
      surveyResult: surveyResultDto.toEntity(),
    );
  }
}
